# coding=utf-8
#

import math

from datetime import timezone
from datetime import datetime
from datetime import timedelta

import volcenginesdkecs
import volcenginesdkcore
from volcenginesdkcore.rest import ApiException

"""
GET /?Action=CreateScheduledInstances&Version=2020-04-01&ZoneId=cn-beijing-a&ScheduledInstanceName=ESI-Name&StartDeliveryAt=2023-03-29T17:50:00+08:00&EndDeliveryAt=2023-03-29T18:10:00+08:00&ImageId=image-3tgt71djf14bwbh7****&InstanceTypeId=ecs.g1ie.xlarge&InstanceName=test-***&Count=1&Password=test@123&NetworkInterfaces.1.SubnetId=subnet-3thbjdy3rh4bwbh8****&NetworkInterfaces.1.SecurityGroupIds.1=sg-3thbinrkp94bwbha****&Volumes.1.VolumeType=ESSD_PL0&Volumes.1.Size=40 HTTP/1.1
Host: open.volcengineapi.com
Region: cn-beijing
ServiceName: ecs
"""


def generate_delivery_time():
    """ """
    # 生成当前交付时间
    tz = timezone(timedelta(hours=8))
    now = datetime.now()
    now = datetime(2024, 6, 19, 3, 30)
    #tomorrow = now + timedelta(days=1) - timedelta(hours=8)
    tomorrow = now

    #tomorrow = datetime(2024, 6, 18, 2)

    cur_minute = now.minute
    s_minute = math.floor(float(cur_minute)/10) * 10

    start_time = datetime(tomorrow.year, tomorrow.month,
                          tomorrow.day, tomorrow.hour, s_minute, tzinfo=tz)
    end_time = start_time + timedelta(seconds=600)
    
    release_time = datetime(now.year, now.month, now.day, 17, tzinfo=tz)

    fmt = "%Y-%m-%dT%H:%M:%S+08:00"
    return start_time.strftime(fmt), end_time.strftime(fmt), release_time.strftime(fmt)


if __name__ == '__main__':
    # 注意示例代码安全，代码泄漏会导致AK/SK泄漏，有极大的安全风险。
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "AKLTZWZjZDE5ZjUwZTNkNDFiM2JiOGVlYjE2ZWFlODBmMmY"
    configuration.sk = "TXpjMU4ySmtZV1V4TnpKaE5HRTNZMkpoTWpVMk9EYzVNelk0TVdFMk1Eaw=="
    configuration.region = "cn-datong"
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    start_time, end_time, release_time = generate_delivery_time()

    print(start_time, end_time, release_time)

    instance_type_id = "ecs.g3al-sn1.4xlarge"
    zone_id = "cn-datong-a"
    image_id = "image-yd7vyl49zsfu03sbevln"
    sg_id = "sg-31atr45r8zri86rnugfrkk8a1"
    sub_id = "subnet-31atr4nij09a86rnugfyzkbz0"

    network_interfaces = [volcenginesdkecs.NetworkInterfaceForRunInstancesInput(subnet_id=sub_id, security_group_ids=[sg_id,])]
    #eip_interfaces = volcenginesdkecs.EipAddressForCreateScheduledInstancesInput(bandwidth_mbps=2,
    #                                                                             charge_type="PayByTraffic",
    #                                                                             release_with_instance=True)

    #data = {"Version": "2020-04-01",
    data = {"zone_id": zone_id,
            "scheduled_instance_name": "ESI-name",
            "start_delivery_at": start_time,
            "end_delivery_at": end_time,
            "image_id": image_id,
            "instance_type_id": instance_type_id,
            "instance_name": "test-ECS-name",
            "auto_release_at": release_time,
            "delivery_type": "Reserve",
            "elastic_scheduled_instance_type": "Segmented",
            "count": 10,
            "network_interfaces": network_interfaces,
            "password": "123qweASD",
            #"eip_address": eip_interfaces,
            #"network_interfaces.N.SecurityGroupIds.N": "NetworkInterfaces.1.SecurityGroupIds.1=%s" % sg_id,
            #"EipAddress.ReleaseWithInstance": True,
            #"Volumes.N.Size": "Volumes.1.Size=40",
            #"NetworkInterfaces.N.SubnetId": "NetworkInterfaces.1.SubnetId=%s" % sub_id,
            }

    print(data)
    # use global default configuration
    api_instance = volcenginesdkecs.ECSApi()
    start_instance_request = volcenginesdkecs.CreateScheduledInstancesRequest(**data)

    # 复制代码运行示例，请自行打印API返回值。
    resp = api_instance.create_scheduled_instances(start_instance_request)
    print(resp)
    import ipdb;ipdb.set_trace()
    print("foo")
